package cn.jxau.fukin.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.jxau.fukin.model.Card;
import cn.jxau.fukin.model.Rate;
import cn.jxau.fukin.model.User;
import cn.jxau.fukin.service.IRateService;
import cn.jxau.fukin.service.IUserService;

/**
 * Description:存款功能
 * Title:DepositController
 * @author ChunFu-Lee
 * @date 2017-9-12 下午5:45:09
 */
@Controller
public class DepositController {

	@Autowired
	IRateService rateService;
	@Autowired
	IUserService userService;

	/**
	 * 存款页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/showDepositPage.do")
	public String showDepositPage(HttpServletRequest request) {
		String flag = (String) request.getSession().getAttribute("flag");
		if (flag != null) {

			return "forward:/WEB-INF/jsp/deposit.jsp";
		} else {
			return "forward:/user/showLoginPage.do";

		}
	}

	@RequestMapping(value = "/deposit.do")
	public ModelAndView deposit(ModelAndView mv, HttpServletRequest request) {
		// 用户名
		String uname = request.getParameter("uName");
		// 卡号
		String cid = request.getParameter("cId");
		// 存款金额
		String money = request.getParameter("Account");
		// 存款类型，活期定期
		String depositType = request.getParameter("DepositType");
		// 存款期限
		String deadline = request.getParameter("depositDeadline");
		// 身份证号
		String cardid = request.getParameter("uCardId");
		// 交易密码
		String password = request.getParameter("cPassword");
		System.out.println("uname=" + uname + ",cid=" + cid + ",money=" + money + ",type="
				+ depositType + ",time=" + deadline + ",cardid=" + cardid + ",password=" + password
				+ "");
		// 封装user参数
		User user = new User();
		user.setuName(uname);
		user.setuCId(cid);
		user.setuCardid(cardid);
		String checkingUserMsg = userService.checkingUser(user);
		if (!("true".equals(checkingUserMsg))) {
			request.getSession().setAttribute("msg", checkingUserMsg);
			mv.setViewName("redirect:/showDepositPage.do");
			return mv;
		}
		// 获取数据库中的user
		User newUser = userService.queryUserInfo(user).get(0);

		Card card = new Card();
		card.setcId(cid);
		card.setcPassword(password);
		card.setUser(newUser);

		Rate rate = new Rate();
		rate.setDepositType(depositType);
		String deposit = rateService.deposit(card, rate, money);
		System.out.println(deposit);
		request.getSession().setAttribute("msg", "存款成功");
		mv.setViewName("redirect:/showDepositPage.do");
		return mv;
	}
}
